<template>
  <div>
    <div id="detail-Wrap">
      <div class="stemInfo">{{stem[0].groupName}}-{{stem[0].gradeName}}-{{stem[0].subject}}</div>
      <div class="doTime">
        <i-icon type="time_fill" size="21" color="#2e8e9a"/>用时：
        {{targetTime}}
      </div>
      <div class="trueStem">正确{{trueTotal}}/{{stem.length}}</div>
    </div>

    <div class="detailBtns">
      <div
        class="stemBtn"
        v-for="(item,index) in stem"
        :key="index"
        :data-idx="index"
        :style="{backgroundColor:detailTotal[index]=='null'?'#dedede':(detailTotal[index]?'#9cd5ad':'#E53059')}"
        @click="handleClick"
      >{{index+1}}</div>
    </div>
    <div class="confirm weui-btn weui-btn_primary" @click="exitBtn">返回查看解析</div>
     <div class="confirm weui-btn weui-btn_primary" @click="tryAgain">再试一次</div>
    <div class="wrongSet">
      <div class="wrongStem" v-for="(item,index) in wrongSet" :key="index">
          {{item}}
        <div class="rightItem"><i-icon type="right" size="21" color="#2e8e9a" />正确答案：{{trueAnswer[index]}}</div>
      <div class="wrongItem"><i-icon type="close" size="21" color="#2e8e9a" />你的答案：{{userAnswer[index]}}</div>
      </div>
    </div>
  </div>
</template>

<script>
import globalStore from "../../stores/global-store";
export default {
  data () {
    return{
       stem:[],
       targetTime:{},
       trueTotal:0,
       detailTotal:[],
       wrongSet:[],
       trueAnswer:[],
       userAnswer:[]
    }
  },
  onLoad(options){
    // 获取题目信息
     this.stem = JSON.parse(decodeURIComponent(options.stem));
    //  获取作答时间
     var time =1800000-JSON.parse(decodeURIComponent(options.targetTime));
     var second=this.zeroFill(''+Math.ceil((time/1000)%60),2)
     var minute=this.zeroFill(''+Math.floor((time/1000)/60),2);
     var hour=this.zeroFill(''+Math.floor((time/1000)/60/24),2);
     this.targetTime=hour+":"+minute+":"+second;
    //  获取作答正确题数
    var outIdx=0;
    this.trueTotal=0;
    while(outIdx<this.stem.length){
       var inIdx=0;
       while(inIdx<this.stem[outIdx].optionList.length){
         if(this.stem[outIdx].optionList[inIdx].data=='right'){
             this.detailTotal[outIdx]=true;//正确
             this.trueTotal++;
              break;
         }
         else if(this.stem[outIdx].optionList[inIdx].data=='wrong'){
           this.detailTotal[outIdx]=false;//错误
             this.wrongSet.push((outIdx+1)+"."+this.stem[outIdx].name);
            this.searchTrueA(this.stem[outIdx]);
             this.userAnswer.push(this.stem[outIdx].optionList[inIdx].content);//将空白答案存储在数组中
          //  globalStore.state.wrongSet.push(this.stem[outIdx].id);
           break;
         }
         else{
            this.detailTotal[outIdx]='null';//没有选择
         }
         inIdx++;
         if(inIdx>=this.stem[outIdx].optionList.length){
            this.wrongSet.push((outIdx+1)+"."+this.stem[outIdx].name);
        this.searchTrueA(this.stem[outIdx]);
        this.userAnswer.push(" ");//将空白答案存储在数组中
         }
       } 
       outIdx++;
     }
  },
  onShow(){

  },
  onUnload: function () {
    /* this.wrongSet=[];
    this.userAnswer=[];
    this.trueAnswer=[]; */
    Object.assign(this.$data, this.$options.data())
    /* wx.reLaunch({
      url: '/pages/home/main'
    }) */
  },
  methods: {
  zeroFill(str,n){
    if(str.length<n){
      str='0'+str;
    }
      return str;
  },
  handleClick(e){
  wx.navigateBack({
    delta: 1, // 回退前 delta(默认为1) 页面
  })
  // prepage.onLoad();
  },
  exitBtn(){//查看解析
    wx.navigateBack({
      delta: 1, // 回退前 delta(默认为1) 页面
    })
  },
  searchTrueA(stem){//查找正确选项
    for(var i=0;i<stem.optionList.length;i++){
      if(stem.optionList[i].isRight){
         this.trueAnswer.push(stem.optionList[i].content);
         break;
      }
    }
  },
  tryAgain(){
    wx.redirectTo({
      url: '/pages/practice/main',
      success: function(res){
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  }
  }
}
</script>

<style scoped lang="less">
#detail-Wrap {
  width: 325px;
  box-shadow: 0px 2px 1px #dedede;
  margin: 10rpx auto;
  height: 120rpx;
  padding: 0 60rpx;
  display: flex;
  flex-wrap: wrap;
  position: relative;
  .stemInfo {
    display: inline-block;
    position: absolute;
    left: 60rpx;
    top: 0;
  }
  .doTime {
    display: inline-block;
    position: absolute;
    right: 60rpx;
    top: 0;
  }
  .trueStem {
    display: inline-block;
    position: absolute;
    left: 60rpx;
    bottom: 0;
  }
}

.detailBtns {
  margin: 0 auto;
  width: 325px;
  // height: 50px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;//元素均匀分布
  .stemBtn {
    border: 1px solid transparent;
    border-radius: 50%;
    display: inline-block;
    padding: 0;
    width: 70rpx;
    // height: 60rpx;
    text-align: center;
    line-height:70rpx;
    margin: 10rpx 20rpx;
  }
}
.confirm {
  width: 325px;
  margin: 10px auto;
  background-color: #2e8e9a;
  // padding: 15rpx 120px;
}
.wrongSet{
   margin: 0 auto;
  width: 325px;
  .wrongStem{
  margin: 0 auto;
  width: 325px;
   border-bottom: 1px solid #dedede;
  box-shadow: 0px 2px 1px #dedede;
  }
}
</style>
